package com.skateboard.reelviewtest;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.airbnb.lottie.LottieAnimationView;
import com.skateboard.reelviewtest.adapter.List3DRotate2Adapter;
import com.skateboard.reelviewtest.adapter.List3DRotateAdapter;
import com.skateboard.reelviewtest.path.BigCardLightBorder2View;
import com.skateboard.reelviewtest.rotate.Rotate3dAnimation;
import com.skateboard.reelviewtest.utils.SimpleUtils;

public class List3Rotate2Activity extends AppCompatActivity implements List3DRotate2Adapter.OnItemClickLitener, View.OnClickListener {
    RecyclerView recyclerView;
    RelativeLayout rootView;
    RelativeLayout layout_imageview;


    RelativeLayout childView ;

    LottieAnimationView animation_view;

    BigCardLightBorder2View liuguang_view;

    RelativeLayout rotatecard_foreground;
    ImageView rotatecard_back;

    float itemViewWidth,itemViewHight;

    private int[] locationItem;
    private int[] locationRoot;
    private float centerX;
    private float centerY;
    private Rotate3dAnimation rotateAnimation1;
    private AnimatorSet scaleAnimator;

    RelativeLayout shadow;

    private boolean isAnimatorRuning;

    public static float SCALE_MAX_X=2.2f;
    public static float SCALE_MAX_Y=2.2f;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list3_rotate2);




        animation_view=findViewById(R.id.animation_view);
        animation_view.setAnimation("hello-world.json");
        animation_view.setImageAssetsFolder("images");


        recyclerView=findViewById(R.id.recyclerView);
        rootView=findViewById(R.id.rootView);
        layout_imageview=findViewById(R.id.layout_imageview);
        List3DRotate2Adapter adapter3D=new List3DRotate2Adapter();
        recyclerView.setAdapter(adapter3D);

        recyclerView.setLayoutManager(new GridLayoutManager(this,2));

        adapter3D.setmOnItemClickLitener(this);

        liuguang_view=findViewById(R.id.liuguang_view);

        rotatecard_foreground=findViewById(R.id.rotatecard_foreground);
        rotatecard_back=findViewById(R.id.rotatecard_back);

        layout_imageview.setOnClickListener(this);

        shadow=findViewById(R.id.shadow);
        shadow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    @Override
    public void onItemClick(View view, int position) {
        itemViewWidth=view.getMeasuredWidth();
        itemViewHight=view.getMeasuredHeight();
        if(isAnimatorRuning){
            return;
        }
        isAnimatorRuning=true;
        doAnimation(view);
    }


    public void doAnimation(View view){
        RelativeLayout relativeLayout= (RelativeLayout) view;
        for(int i=0;i<relativeLayout.getChildCount();i++){
            View child=relativeLayout.getChildAt(i);
            if(child.getId()==R.id.item_1){
                childView= (RelativeLayout) child;
            }
        }
        if(childView==null){
            return;
        }
        //定位
        locationItem = new int[2];
        childView.getLocationOnScreen(locationItem);
        locationRoot = new int[2];
        rootView.getLocationOnScreen(locationRoot);
        layout_imageview.setX(locationItem[0]- locationRoot[0]);
        layout_imageview.setY(locationItem[1]- locationRoot[1]);
        layout_imageview.setVisibility(View.VISIBLE);
        shadow.setVisibility(View.VISIBLE);
        childView.setVisibility(View.GONE);
        trans();

    }

    int viewWidth=329;
    int viewHeight=524;
    private void trans(){
        //平移
        centerX = SimpleUtils.getScreenWidth(this)/2;
        centerY = SimpleUtils.getScreenHight(this)/2;
        final AnimatorSet translationAnimatorSet = new AnimatorSet();
        translationAnimatorSet.playTogether(
                ObjectAnimator.ofFloat(layout_imageview, "translationX", centerX -SimpleUtils.dp2px(getApplication(),viewWidth)/2)
                        .setDuration(1000),
                ObjectAnimator.ofFloat(layout_imageview, "translationY", centerY -SimpleUtils.dp2px(getApplication(),viewHeight)/2)
                        .setDuration(1000)
        );
        translationAnimatorSet.start();
        translationAnimatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                //翻转 放大 lottie
                rotateAndScale();
            }
        });
    }
//    private void translate(){
//        final AnimatorSet translationAnimatorSet = new AnimatorSet();
//        translationAnimatorSet.playTogether(
//                ObjectAnimator.ofFloat(layout_imageview, "translationX", locationItem[0]-locationRoot[0])
//                        .setDuration(0),
//                ObjectAnimator.ofFloat(layout_imageview, "translationY", locationItem[1]- locationRoot[1])
//                        .setDuration(0)
//        );
//        translationAnimatorSet.start();
//    }

    private void rotateAndScale() {
        animation_view.postDelayed(new Runnable() {
            @Override
            public void run() {
                animation_view.setVisibility(View.VISIBLE);
                animation_view.playAnimation();
            }
        },2000);
        animation_view.addAnimatorListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                animation_view.setVisibility(View.GONE);
            }
        });

        //从0到90度，顺时针旋转视图，此时reverse参数为true，达到90度时动画结束时视图变得不可见，
        rotateAnimation1 = new Rotate3dAnimation(this, 0f, 360f,centerX
                , centerY, 1f, true);
        rotateAnimation1.setDuration(2500);
        rotateAnimation1.setFillAfter(true);
        rotateAnimation1.setInterpolator(new LinearInterpolator());
        rotateAnimation1.setRotateDegreesCallback(new Rotate3dAnimation.RotateDegreesCallback() {
            @Override
            public void rotateDegressValue(float degress) {
                if (degress >=90.0&&degress<=270.0) {
                    if(rotatecard_back.getVisibility()!=View.VISIBLE){
                        rotatecard_back.setVisibility(View.VISIBLE);
                        rotatecard_foreground.setVisibility(View.INVISIBLE);
                    }
                } else {
                    if(rotatecard_back.getVisibility()!=View.INVISIBLE){
                        rotatecard_back.setVisibility(View.INVISIBLE);
                        rotatecard_foreground.setVisibility(View.VISIBLE);
                    }
                }
            }
        });
        layout_imageview.startAnimation(rotateAnimation1);
        scaleAnimator = new AnimatorSet();
        scaleAnimator.playTogether(
                ObjectAnimator.ofFloat(layout_imageview, "scaleX", 1f, SCALE_MAX_X).setDuration(4000),
                ObjectAnimator.ofFloat(layout_imageview, "scaleY", 1f, SCALE_MAX_Y).setDuration(4000)
        );
        scaleAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                liuguang_view.setVisibility(View.VISIBLE);
                liuguang_view.setX(centerX-liuguang_view.getMeasuredWidth()/2);
                liuguang_view.setY(centerY-liuguang_view.getMeasuredHeight()/2);
                liuguang_view.startAnimation();
                isAnimatorRuning=false;
            }
        });
        //scaleAnimator.start();



    }

    @Override
    public void onClick(View v) {
        if(isAnimatorRuning){
            return;
        }
        isAnimatorRuning=true;
        liuguang_view.setVisibility(View.INVISIBLE);
        //从0到90度，顺时针旋转视图，此时reverse参数为true，达到90度时动画结束时视图变得不可见，
        Rotate3dAnimation rotateAnimation2 = new Rotate3dAnimation(this, 360f, 0f,centerX
                , centerY, 1f, true);
        rotateAnimation2.setDuration(2500);
        rotateAnimation2.setFillAfter(true);
        rotateAnimation2.setStartOffset(1500);
        rotateAnimation2.setInterpolator(new LinearInterpolator());
        rotateAnimation2.setRotateDegreesCallback(new Rotate3dAnimation.RotateDegreesCallback() {
            @Override
            public void rotateDegressValue(float degress) {
                if (degress >=90.0&&degress<=270.0) {
                    if(rotatecard_back.getVisibility()!=View.VISIBLE){
                        rotatecard_back.setVisibility(View.VISIBLE);
                        rotatecard_foreground.setVisibility(View.INVISIBLE);
                    }
                } else {
                    if(rotatecard_back.getVisibility()!=View.INVISIBLE){
                        rotatecard_back.setVisibility(View.INVISIBLE);
                        rotatecard_foreground.setVisibility(View.VISIBLE);
                    }
                }
            }
        });


        scaleAnimator = new AnimatorSet();
        scaleAnimator.playTogether(
                ObjectAnimator.ofFloat(layout_imageview, "scaleX", SCALE_MAX_X, 1f).setDuration(4000),
                ObjectAnimator.ofFloat(layout_imageview, "scaleY", SCALE_MAX_Y, 1f).setDuration(4000)
        );
        scaleAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                reTrans();
            }
        });
        scaleAnimator.start();
        layout_imageview.startAnimation(rotateAnimation2);

    }
    private void reTrans(){
        final AnimatorSet translationAnimatorSet = new AnimatorSet();
        translationAnimatorSet.playTogether(
                ObjectAnimator.ofFloat(layout_imageview, "translationX", locationItem[0]-locationRoot[0])
                        .setDuration(2000),
                ObjectAnimator.ofFloat(layout_imageview, "translationY", locationItem[1]-locationRoot[1])
                        .setDuration(2000)
        );
        translationAnimatorSet.start();
        translationAnimatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                layout_imageview.post(new Runnable() {
                    @Override
                    public void run() {
                        layout_imageview.setX(-1000);
                        childView.setVisibility(View.VISIBLE);
                        shadow.setVisibility(View.GONE);
                        isAnimatorRuning=false;
                    }
                });
            }
        });
    }
}